package com.crane.app.web.controller;


import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.SingleResponse;
import com.crane.common.core.utils.BaseContextHandler;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.app.bean.req.AppQueryStudentClassCourseReq;
import com.crane.oss.app.bean.req.AppStudentPhysicalReportReq;
import com.crane.oss.app.service.AppUserService;
import com.crane.oss.school.bean.resp.StudentResp;
import com.crane.oss.school.bean.resp.TeacherResp;
import com.crane.oss.school.service.StudentService;
import com.crane.oss.school.service.TeacherService;
import com.crane.oss.sports.XunFeiAppApiService;
import com.crane.oss.sports.XunFeiPrescriptionApiService;
import com.crane.oss.sports.bean.response.exerciseprescription.ClassPrescriptionResp;
import com.crane.oss.sports.bean.response.exerciseprescription.ItemScoreBean;
import com.crane.oss.sports.bean.response.exerciseprescription.LevelBmiBean;
import com.crane.oss.sports.bean.resquest.QueryClassExercisePrescriptionReq;
import com.crane.oss.xunfei.req.QueryAppStudentPhysicalReportReq;
import com.crane.oss.xunfei.req.QueryPrescriptionClassInfoReq;
import com.crane.oss.xunfei.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zx
 */
@Api(value = "AppPhysicalReportController", tags = {"体质健康报告"})
@RestController
@RequestMapping("/app/physicalReport")
public class AppPhysicalReportController {

    @Resource
    AppUserService appUserService;

    @Resource
    TeacherService teacherService;

    @Resource
    StudentService studentService;

    @Resource
    XunFeiAppApiService xunFeiAppApiService;

    @Autowired
    XunFeiPrescriptionApiService xunFeiPrescriptionApiService;


    @ApiOperation(value = "家长-学生体质健康报告", notes = "家长-学生体质健康报告")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AppStudentPhysicalReportReq")
    @PostMapping("/students")
    public SingleResponse<AppStudentPhysicalReportResp> getStudentsPhysicalReport(@RequestBody AppStudentPhysicalReportReq request) {
        Long userId= BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        QueryAppStudentPhysicalReportReq queryAppAnalysisStudentClassCourseReq=new QueryAppStudentPhysicalReportReq();
        queryAppAnalysisStudentClassCourseReq.setStudentId(studentResp.getStudentId().toString());
        queryAppAnalysisStudentClassCourseReq.setSportCycleId(request.getSportCycleId());
        ApiSingleResponse<AppStudentPhysicalReportResp> response=xunFeiAppApiService.getAppStudentPhysicalReport(queryAppAnalysisStudentClassCourseReq,studentResp.getPartyId().toString(),null,userId.toString());
        if(null != response && response.getMsg().equals("success") && null !=response.getData()){
            response.getData().setImg(studentResp.getImg());
            response.getData().setAge(studentResp.getAge());
            response.getData().setStudentName(studentResp.getName());
            response.getData().setGender(studentResp.getSex()+1);
            return SingleResponse.of(response.getData());
        }
        return SingleResponse.buildFailure("请求失败");
    }


    @ApiOperation(value = "老师-学生体质健康报告", notes = "老师-学生体质健康报告")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AppStudentPhysicalReportReq")
    @PostMapping("/student")
    public SingleResponse<AppStudentPhysicalReportResp> getStudentPhysicalReport(@RequestBody AppStudentPhysicalReportReq request) {
        Long userId= BaseContextHandler.getUserId();
        TeacherResp teacherResp=teacherService.queryTeacherByAppUserId(userId);
        QueryAppStudentPhysicalReportReq queryAppAnalysisStudentClassCourseReq=new QueryAppStudentPhysicalReportReq();
        BeanUtil.copyProperties(request,queryAppAnalysisStudentClassCourseReq);
        ApiSingleResponse<AppStudentPhysicalReportResp> response=xunFeiAppApiService.getAppStudentPhysicalReport(queryAppAnalysisStudentClassCourseReq,teacherResp.getPartyId().toString(),null,userId.toString());
        if(null != response && response.getMsg().equals("success") && null !=response.getData()){
            StudentResp studentResp=studentService.queryStudent(Long.valueOf(request.getStudentId()));
            response.getData().setImg(studentResp.getImg());
            response.getData().setAge(studentResp.getAge());
            response.getData().setStudentName(studentResp.getName());
            response.getData().setGender(studentResp.getSex()+1);
            return SingleResponse.of(response.getData());
        }
        return SingleResponse.buildFailure("请求失败");
    }


    @ApiOperation(value = "老师-班级体质健康报告", notes = "老师-班级体质健康报告")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryClassExercisePrescriptionReq")
    @PostMapping("/class")
    public SingleResponse<ClassPrescriptionResp> classList(@RequestBody QueryClassExercisePrescriptionReq request) {

        Long userId= BaseContextHandler.getUserId();
        TeacherResp teacherResp=teacherService.queryTeacherByAppUserId(userId);

        ClassPrescriptionResp classPrescriptionResp=new ClassPrescriptionResp();
        QueryPrescriptionClassInfoReq queryTestCycleReq =new QueryPrescriptionClassInfoReq();
        queryTestCycleReq.setClassId(request.getClassId());
        queryTestCycleReq.setSportCycleId(request.getCycleId());
        ApiSingleResponse<PrescriptionClassInfoResp> prescriptionClassInfo= xunFeiPrescriptionApiService.getPrescriptionClassInfo(queryTestCycleReq,teacherResp.getPartyId().toString(), null,teacherResp.getTeacherId().toString());
        if(prescriptionClassInfo.getMsg().equals("success")){
            classPrescriptionResp.setAvgScore(prescriptionClassInfo.getData().getAvgScore());
            classPrescriptionResp.setAvgAddScore(prescriptionClassInfo.getData().getAvgAddScore());
            classPrescriptionResp.setSchoolExcellentScore(prescriptionClassInfo.getData().getSchoolExcellentScore());
            classPrescriptionResp.setSchoolPassScore(prescriptionClassInfo.getData().getSchoolPassScore());
        }


        ApiSingleResponse<PrescriptionClassScoreStatisticResp> prescriptionClassScoreStatistic=xunFeiPrescriptionApiService.getPrescriptionClassScoreStatistic(queryTestCycleReq,teacherResp.getPartyId().toString(), null,teacherResp.getTeacherId().toString());
        if(prescriptionClassInfo.getMsg().equals("success") && null != prescriptionClassScoreStatistic.getData()){
            classPrescriptionResp.setExcellent(prescriptionClassScoreStatistic.getData().getExcellent());
            classPrescriptionResp.setGood(prescriptionClassScoreStatistic.getData().getGood());
            classPrescriptionResp.setPass(prescriptionClassScoreStatistic.getData().getPass());
            classPrescriptionResp.setUnpass(prescriptionClassScoreStatistic.getData().getUnpass());
        }

        ApiListResponse<PrescriptionBmiResp> prescriptionBmiRespApiListResp= xunFeiPrescriptionApiService.getPrescriptionBmi(queryTestCycleReq,teacherResp.getPartyId().toString(), null,teacherResp.getTeacherId().toString());
        if(prescriptionBmiRespApiListResp.getMsg().equals("success")){
            Map<String, LevelBmiBean> bmi=new HashMap<>();
            List<LevelBmiBean> bmiList= BeanUtil.copyList(prescriptionBmiRespApiListResp.getData(),LevelBmiBean.class);
            for(LevelBmiBean levelBmiBean : bmiList){
                if(levelBmiBean.getLevelKey().equals("肥胖")){
                    bmi.put("fat",levelBmiBean);
                }else if(levelBmiBean.getLevelKey().equals("超重")){
                    bmi.put("more",levelBmiBean);
                }else if(levelBmiBean.getLevelKey().equals("低体重")){
                    bmi.put("low",levelBmiBean);
                }else if(levelBmiBean.getLevelKey().equals("正常")){
                    bmi.put("normal",levelBmiBean);
                }
            }
            classPrescriptionResp.setBmi(bmi);
        }
        ApiListResponse<PrescriptionItemResp> itemResp= xunFeiPrescriptionApiService.getPrescriptionItem(queryTestCycleReq,teacherResp.getPartyId().toString(), null,teacherResp.getTeacherId().toString());
        if(itemResp.getMsg().equals("success")){
            List<ItemScoreBean> itemScore=itemResp.getData().stream().map(s ->{
                ItemScoreBean a=new ItemScoreBean();
                a.setItemName(s.getItemName());
                a.setFail(s.getFail());
                a.setAvgScore(s.getAvgScore());
                a.setPass(s.getPass());
                a.setExcellent(s.getExcellent());
                a.setGood(s.getGood());
                return  a;
            }).collect(Collectors.toList());
            classPrescriptionResp.setItemScore(itemScore);
        }
        return SingleResponse.of(classPrescriptionResp);
    }

}
